/**
 * Filename: SwitchPhotoAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/5/8 15:25
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.LocalMusicManageActivity;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.utils.ImageOptHelper;

import java.util.List;

public class SwitchPhotoAdapter extends BaseAdapter {

    private List<String> mDatas;
    private Context context;

    public SwitchPhotoAdapter(Context context, List<String> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return mDatas.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = View.inflate(context, R.layout.yn_include_add_photo, null);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, LocalMusicManageActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    context.startActivity(intent);
                    Intent mIntent = new Intent(ACTIONs.actionUpdateUpNum);
                    context.sendBroadcast(mIntent);
                }
            });
            return convertView;
        }

        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.yn_item_grid_image, null);
            viewHolder.iv_group_grid_image = (SimpleDraweeView) convertView
                    .findViewById(R.id.iv_group_grid_image);
            viewHolder.iv_group_grid_delete_image = (ImageView) convertView
                    .findViewById(R.id.iv_group_grid_delete_image);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String items = mDatas.get(position);
        Uri uri = Uri.parse("file://" + items);
        ImageOptHelper.showFrescoNative(uri, viewHolder.iv_group_grid_image, context);

        viewHolder.iv_group_grid_delete_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDatas.remove(position);
                SelectedImg.mSelectedImage.remove(position);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    public static class ViewHolder {
        public SimpleDraweeView iv_group_grid_image;
        public ImageView iv_group_grid_delete_image;
    }
}
